home *** CD-ROM | disk | FTP | other *** search
- /*
- * SFcolours - Star Fighter 3000 colours editor
- * Utility functions
- * Copyright (C) 2001 Chris Bazley
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public Licence as published by
- * the Free Software Foundation; either version 2 of the Licence, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public Licence for more details.
- *
- * You should have received a copy of the GNU General Public Licence
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- /* ANSI library files */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <limits.h>
-
- /* RISC OS library files */
- #include "kernel.h"
- #include "toolbox.h"
- #include "event.h"
- #include "window.h"
- #include "wimplib.h"
- #include "gadgets.h"
- #include "swis.h"
- #include "flex.h"
-
- /* My library files */
- #include "ViewsMenu.h"
- #include "err.h"
- #include "msgtrans.h"
- #include "hourglass.h"
- #include "Macros.h"
- #include "FilePerc.h"
-
- /* Local headers */
- #include "Utils.h"
- #include "Main.h"
-
- #ifdef INT_COLOUR_FIND
- /* OS 2 ColourTrans used R2,G3,B1 and OS 3 ColourTrans uses R2,G4,B1 */
- #define RED_WEIGHT 2
- #define GREEN_WEIGHT 4
- #define BLUE_WEIGHT 1
- #define SQUARE(n) ((n)*(n))
- #endif
-
- /* ----------------------------------------------------------------------- */
- /* Public functions */
-
- char brightness_of_24bit_col(int colour)
- {
- int r,g,b;
-
- r = (colour & 0x0000ff00) >> 8;
- g = (colour & 0x00ff0000) >> 16;
- b = (colour & 0xff000000) >> 24;
- /* CIE luminance weights for red, green and blue (0.299, 0.587, 0.114), which is the standard conversion for broadcast television */
- return (char)((int)(0.299*(float)r) + (int)(0.587*(float)g) + (int)(0.114*(float)b));
- }
-
- /* ----------------------------------------------------------------------- */
-
- void set_24bit_button_col(ObjectId window, ComponentId button, int colour)
- {
- /* Set background colour of button gadget to a 24-bit colour */
- char validation[16];
-
- sprintf(validation, "C/%X", (colour & 0xffffff00) >> 8);
- RE(button_set_validation(0, window, button, validation));
- if(brightness_of_24bit_col(colour) > 128) {
- RE(button_set_flags(0, window, button, WimpIcon_FGColour*0x0f, WimpIcon_FGColour*0x07));
- }
- else {
- RE(button_set_flags(0, window, button, WimpIcon_FGColour*0x0f, WimpIcon_FGColour*0x00));
- }
- }
-
- /* ----------------------------------------------------------------------- */
-
- _kernel_oserror *open_topleftofwin(unsigned int flags, ObjectId showobj, ObjectId relativeto, ObjectId parent, ComponentId parent_component)
- {
- WimpGetWindowStateBlock winstate;
- WindowShowObjectBlock showblock;
-
- THROW(window_get_wimp_handle(0, relativeto, &(winstate.window_handle)));
- THROW(wimp_get_window_state(&winstate));
- showblock.visible_area.xmin = winstate.visible_area.xmin+64;
- showblock.visible_area.ymin = winstate.visible_area.ymax-64;
- return toolbox_show_object(flags, showobj, Toolbox_ShowObject_TopLeft, &showblock, parent, parent_component);
- }
-
- /* ----------------------------------------------------------------------- */
-
- _kernel_oserror *show_win_at_ptr(unsigned int flags, ObjectId id, ObjectId parent, ComponentId parent_component)
- {
- unsigned int state;
-
- THROW(toolbox_get_object_state(0, id, &state))
- if(FLAG_SET(state, Toolbox_GetObjectState_Showing))
- /* Already open */
- THROW(ViewsMenu_show_object(flags, id, Toolbox_ShowObject_Default, NULL, parent, parent_component))
- else
- /* Not open (can't very well be iconised!) */
- THROW(toolbox_show_object(flags, id, Toolbox_ShowObject_AtPointer, NULL, parent, parent_component))
-
- return NULL; /* success */
- }
-
- /* ----------------------------------------------------------------------- */
-
- char *tail(char *pathname, int length)
- {
- char *ptr;
- int dotcount;
-
- ptr = (char *)((int)pathname + strlen(pathname)); /* terminator */
- dotcount = 0;
- while(ptr > pathname && dotcount<length) {
- ptr--; /* scan string backwards from terminator */
- if(*ptr == '.')
- dotcount++;
- }
- if(dotcount >= length)
- return (char *)((int)ptr + 1);
- return ptr;
- }
-
- /* ----------------------------------------------------------------------- */
-
- char real_to_mode13col(unsigned int real_col)
- {
- /* Find nearest to ideal colour in default mode 13 palette */
- #ifdef INT_COLOUR_FIND
- if(use_colour_trans) {
- #endif
- int nearest_colour;
- RE(_swix(ColourTrans_ReturnColourNumberForMode, _INR(0,2)|_OUT(0), real_col, 13, 0, &nearest_colour))
- return nearest_colour;
- #ifdef INT_COLOUR_FIND
- } else {
- /* Like ColourTrans we use a least squares function
- but we have control over the weights */
- char best_col;
- int least_dist = INT_MAX;
- int target_red = (real_col & 0xff00)>>8;
- int target_green = (real_col & 0xff0000)>>16;
- int target_blue = (real_col & 0xff000000)>>24;
- for(int colnum = 0; colnum <= 255; colnum++) {
- int dist = RED_WEIGHT*SQUARE(((palette[colnum] & 0xff00)>>8) - target_red)
- + GREEN_WEIGHT*SQUARE(((palette[colnum] & 0xff0000)>>16) - target_green)
- + BLUE_WEIGHT*SQUARE(((palette[colnum] & 0xff000000)>>24) - target_blue);
- if(dist < least_dist) {
- least_dist = dist;
- best_col = colnum;
- }
- }
- return best_col;
- }
- #endif
- }
-
- /* ----------------------------------------------------------------------- */
-
- _kernel_oserror *load_compressed(char *filepath, flex_ptr buffer_anchor)
- {
- return perc_operation(FILEPERC_OP_DECOMP, filepath, 0, buffer_anchor);
- }
-